import tkinter
import chinese_calendar
import pandas as pd
import datetime
import random
import os

# per_month_salary_lst_1 = [580, 580, 580, 580, 580, 580, 580, 580, 580, 900, 880]
# per_month_salary_lst_2 = [600, 600, 600, 600, 600, 600, 600, 600, 600, 800, 800]

# print(len(per_month_salary_lst_1), len(per_month_salary_lst_2))
try:
    year = int(input("请输入年份："))
except:
    exit("请输入正确的年份")

start_time = datetime.date(year, 1, 1)  # 指定开始时间
end_time = datetime.date(year, 12, 31)  # 指定结束时间

workdays_lst = []
holidays_lst = []

workdays = chinese_calendar.get_workdays(start_time, end_time)
for day in workdays:
    workdays_lst.append(day.strftime('%Y-%m-%d'))

holidays = chinese_calendar.get_holidays(start_time, end_time)
for day in holidays:
    holidays_lst.append(day.strftime('%Y-%m-%d'))

print("工作日：", workdays_lst, "\n")
print("节假日：", holidays_lst)

workdays_salary = 60
holidays_salary = 80


class workdayOverTime:
    def __init__(self, workdays_lst, workdays_salary):
        self.workdays_lst = workdays_lst
        self.workdays_salary = workdays_salary

    def get_workday_per_month_salary(self, month):
        workday_per_month_salary = []
        for workday in self.workdays_lst:
            try:
                date = datetime.datetime.strptime(workday, '%Y-%m-%d')
                if date.month == month:
                    workday_per_month_salary.append(workday)
            except ValueError:
                print(f"无效的日期格式: {workday}")
        return workday_per_month_salary

    def random_choice_date(self, get_month_lst, get_num):
        random_lst = []
        self.get_month_lst = get_month_lst
        while len(random_lst) < get_num:
            random_day = random.choice(self.get_month_lst)
            random_day = datetime.datetime.strptime(random_day, '%Y-%m-%d')
            random_day = random_day.day
            if random_day not in random_lst:
                random_lst.append(random_day)
                continue
        random_lst.sort()
        return str(random_lst)


class holidayOverTime:
    def __init__(self, holidays_lst, holidays_salary):
        self.holidays_lst = holidays_lst
        self.holidays_salary = holidays_salary

    def get_holiday_per_month_salary(self, month):
        holiday_per_month_salary = []
        for holiday in self.holidays_lst:
            try:
                date = datetime.datetime.strptime(holiday, '%Y-%m-%d')
                if date.month == month:
                    holiday_per_month_salary.append(holiday)
            except ValueError:
                print(f"无效的日期格式: {holiday}")
        return holiday_per_month_salary

    def random_choice_date(self, get_month_lst, get_num):
        random_lst = []
        self.get_month_lst = get_month_lst
        while len(random_lst) < get_num:
            random_day = random.choice(self.get_month_lst)
            random_day = datetime.datetime.strptime(random_day, '%Y-%m-%d')
            random_day = random_day.day
            if random_day not in random_lst:
                random_lst.append(random_day)
                continue
        random_lst.sort()
        return str(random_lst)


if __name__ == '__main__':
    to_excel = pd.DataFrame
    wdot = workdayOverTime(workdays_lst, workdays_salary)
    hdot = holidayOverTime(holidays_lst, holidays_salary)

    data = []
    # print(wdot.get_workday_per_month_salary(11))
    # print(hdot.get_holiday_per_month_salary(1))

    for j in range(12):
        print(f"-> 生成{j + 1}月工作日")
        for i in range(8):
            per_month_lst = wdot.get_workday_per_month_salary(int(j + 1))
            random_lst = wdot.random_choice_date(per_month_lst, 7)
            # print(random_lst)
            data.append({"月份": j + 1, "类型": "工作日", "日期": random_lst})
        for i in range(2):
            per_month_lst = wdot.get_workday_per_month_salary(int(j + 1))
            random_lst = hdot.random_choice_date(per_month_lst, 6)
            # print(random_lst)
            data.append({"月份": j + 1, "类型": "工作日", "日期": random_lst})
        print(f"-> 生成{j + 1}月节假日")
        for i in range(8):
            per_month_lst = hdot.get_holiday_per_month_salary(int(j + 1))
            random_lst = hdot.random_choice_date(per_month_lst, 2)
            # print(random_lst)
            data.append({"月份": j + 1, "类型": "节假日", "日期": random_lst})
        for i in range(2):
            per_month_lst = hdot.get_holiday_per_month_salary(int(j + 1))
            random_lst = wdot.random_choice_date(per_month_lst, 3)
            # print(random_lst)
            data.append({"月份": j + 1, "类型": "节假日", "日期": random_lst})

    # 将数据导出到 Excel
    df = pd.DataFrame(data)

    # 文本格式化
    df["日期"] = df["日期"].str.replace("[", "")
    df["日期"] = df["日期"].str.replace("]", "")
    df["日期"] = df["日期"].str.replace(" ", "")
    df["日期"] = df["日期"].str.replace(",", "、")

    df.to_excel(f"{year}年度加班数据.xlsx", index=False)

    print("\n-> 数据已成功导出")
    os.startfile(f"{year}年度加班数据.xlsx")
